/* GSOrderApplyProcessor.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2009-3-26	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.business;

import java.sql.Connection;
import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSOrderApplyBean;
import com.hitrust.gs.dao.GSOrderApplyDao;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSBusOrderCancelVO;
import com.hitrust.gs.vo.GSOrderAuthorizeSaveVO;

public class GSOrderApplyProcessor implements GSBaseProcesser {
    private GSOrderApplyDao applyDao=new GSOrderApplyDao();
    
    public ArrayList getAllOrderApplyByPage(GSBusOrderCancelVO aGSBusOrderCancelVO,int pageNo,int pageSize) throws Exception{
        ArrayList tList=applyDao.getAllOrderApplyByPage(aGSBusOrderCancelVO, pageNo, pageSize);
        return tList;
    }
    
    public int getAllOrderApplyCount(GSBusOrderCancelVO aGSBusOrderCancelVO) throws Exception{
        return applyDao.getAllOrderApplyCount(aGSBusOrderCancelVO);
    }
    
    public GSOrderApplyBean getOrderApplyBeanById(String aId)throws Exception{
        GSOrderApplyBean bean= applyDao.getOrderApplyBeanById(aId);
        if (bean==null) {
            throw new GSException("根据编号查询捕获申请信息错误！");
        }
        return bean;
    }
    //拒绝申请
    public String orderApplyRebuff(String[] ids)throws Exception{
        GSCommunicationApepProcessor apeppro=new GSCommunicationApepProcessor();
        Connection con=null;
        String msg="操作执行成功！";
        try {
            con.setAutoCommit(false);
            ArrayList tList=new ArrayList();
            for (int i = 0; i < ids.length; i++) {
                GSOrderApplyBean bean=this.getOrderApplyBeanById(ids[i]);
                //更新申请单
                boolean bl=applyDao.updateOrderApplyBean(con, bean);
                tList.add(bean);
            }
          //发送请求
            String result=apeppro.applyNoAggreApep(tList);
            String[] res=GSCommunicationApepProcessor.parseReturnXml(result);
            if(res[0].equals(GSBaseConstants.APEP_RETURN_SIGN_SUCCESS)){
                con.commit();
            }else{
                msg="拒绝订单执行失败,"+res[1];
                con.rollback();
            }
        } catch (Exception e) {
            GSLogWriter.error(e.getMessage());
            throw e;
        }finally{
            applyDao.close(con);
        }
        return msg;
    }
    //补货申请转为申购单并提交
    public String insertApply(LoginContext context,ArrayList tList)throws Exception{
        GSBusOrderProcesser opro=new GSBusOrderProcesser();
        GSOrderDAO ordao=new  GSOrderDAO();
        String[] orderIds=ordao.insertApply(context, tList);
        String[] codes=new String[tList.size()];
        ArrayList tempList=new ArrayList();
        for (int i = 0; i < orderIds.length; i++) {
            GSOrderAuthorizeSaveVO vo=new GSOrderAuthorizeSaveVO();
            vo.detailId=orderIds[i];
            tempList.add(vo);
        }
        for (int j = 0; j < tList.size(); j++) {
            GSOrderApplyBean bean=(GSOrderApplyBean) tList.get(j);
            codes[j]=bean.getApply_code();
        }
        String msg=opro.orderSubmitNewToApep(context,tempList,codes);
        return msg;
    }
}
